<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/12/12
 * Time: 22:39
 */

namespace app\modules\iterator\controller;

use app\modules\iterator\Book;
use app\modules\iterator\BookList;
use yii\web\Controller;

class DefaultController extends Controller
{
    /**
     * 迭代器模式示例
     * 让对象变得可迭代并表现得像对象集合
     *
     * @date   2018/12/13
     * @author edmmss
     * @return bool
     */
    public function actionIndex()
    {
        $bookList = new BookList();
        $bookList->addBook(new Book('edmmss', 'PHP'));
        $bookList->addBook(new Book('edmmss', 'JAVA'));
        $bookList->addBook(new Book('edmmss', 'mysql'));

        $books = [];
        foreach ($bookList as $book) {
            $books[] = $book->getAuthorAndTitle();
        }

        var_dump($books);

        return true;
    }
}